package com.insigma.cisco.itc.tms.teammanagement.action;

import org.apache.struts2.json.annotations.JSON;

import com.insigma.cisco.itc.tms.common.action.BaseAction;
import com.insigma.cisco.itc.tms.common.domain.User;
import com.insigma.cisco.itc.tms.teammanagement.service.LoginService;
import com.opensymphony.xwork2.ActionContext;

public class LoginAction extends BaseAction {

	private static final long serialVersionUID = 5320050254360655555L;

	private String username;

	private String password;

	private String result;

	private LoginService loginService;

	public String login() {
		User user = loginService.getUser(username, password);
		if (user == null) {
			this.setResult("fail");
		} else {
			this.putSession(user);
			this.setResult("success");

		}
		return JSON;
	}

	public String logout() {
		session.clear();
		return INPUT;
	}
	
	public String showLogin(){
		return LOGIN;
	}

	private void putSession(User user) {
		session = ActionContext.getContext().getSession();
		session.put("user", user);
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}

	@JSON(serialize = false)
	public LoginService getLoginService() {
		return loginService;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

}
